home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_052 / hampoly / hampoly.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  209 lines

  1. /****************************************************************
  2.  * Polygon drawing demo by John M. Olsen
  3.  * This demo uses the AreaMove, AreaDraw and AreaEnd Functions.
  4.  * It uses a hold and modify (HAM) screen, and the polygons can be
  5.  * any of the possible 4096 colors.  This is done by using a patterned
  6.  * area fill, using separate patterns to modify the red, green, and blue
  7.  * to get to the correct color as fast as possible.
  8.  *
  9.  * My appologies for the sparceness of comments.  It is hopefully clear
  10.  * what I was doing for the most part.
  11.  *
  12.  * This is meant to be compiled with the Manx compiler, and has not been
  13.  * tried under Lettuce.
  14.  *
  15.  * © 1987 John M. Olsen
  16.  *
  17.  * Permission is given to distribute this code wherever you want as long
  18.  * as this notice remains with it.  Do not use any part of it in a commercial
  19.  * application without written consent of the author:
  20.  *
  21.  * John M. Olsen
  22.  * 1547 Jamestown Drive
  23.  * Salt Lake City, UT  84121
  24.  ****************************************************************/
  25.  
  26. #include <exec/types.h>
  27. #include <graphics/gfxbase.h>
  28. #include <graphics/gfxmacros.h>
  29. #include <graphics/rastport.h>
  30. #include <intuition/intuition.h>
  31. #include <stdio.h>
  32.  
  33. void *OpenLibrary();
  34. struct Window *OpenWindow(), *w;
  35. struct Screen *OpenScreen(), *s;
  36. struct IntuiMessage *GetMsg(), *msg;
  37.  
  38. struct IntuitionBase *IntuitionBase;
  39. struct GfxBase *GfxBase;
  40.  
  41. BYTE *AllocRaster();
  42. WORD areabuffer[250];
  43. struct TmpRas tmpras;
  44. struct AreaInfo myAreaInfo;
  45.  
  46. USHORT a[] = { 0x9429, 0x4294 };
  47. USHORT b[] = { 0x4294, 0x2942 };
  48.  
  49. /*****************************************************************
  50. definition of the 320 X 200 HAM screen.
  51. *****************************************************************/
  52. struct NewScreen ss =
  53. {                /*****************/
  54.     0,            /* LeftEdge      */
  55.     0,            /* TopEdge       */
  56.     320,            /* Width         */
  57.     200,            /* Height        */
  58.     6,            /* Depth         */
  59.     0,            /* DetailPen     */
  60.     1,            /* BlockPen      */
  61.     HAM,            /* ViewModes     */
  62.     CUSTOMSCREEN,        /* Type          */
  63.     NULL,            /* *Font         */
  64.     (UBYTE *) "H.A.M. Screen",    /* *DefaultTitle */
  65.     NULL,            /* *Gadgets      */
  66.     NULL            /* *CustomBitMap */
  67. };                /*****************/
  68.  
  69. struct NewWindow ww =
  70. {                /****************/
  71.     0,            /* LeftEdge    */
  72.     10,            /* TopEdge    */
  73.     200,            /* Width    */
  74.     150,            /* Height    */
  75.     -1,            /* DetailPen    */
  76.     -1,            /* BlockPen    */
  77.     CLOSEWINDOW,        /* IDCMP    */
  78.     WINDOWCLOSE | ACTIVATE | NOCAREREFRESH | WINDOWDRAG | WINDOWDEPTH
  79.     | WINDOWSIZING,
  80.     NULL,            /* *FirstGadget    */
  81.     NULL,            /* *CheckMark    */
  82.     (UBYTE *)"H.A.M. Poly Window",    /* *Title    */
  83.     NULL,            /* Screen    */
  84.     NULL,            /* *Bitmap    */
  85.     100,40,320,200,        /* Min/Max w,h    */
  86.     CUSTOMSCREEN        /* Type        */
  87. };
  88.  
  89. main(argc,argv)
  90. int argc;
  91. char *argv[];
  92. {
  93.     setup();
  94.     drawstuff();
  95.     die(0);
  96. }
  97.  
  98. setup()
  99. {
  100.     if(!(GfxBase = OpenLibrary("graphics.library",0l)))
  101.         die(1);
  102.     if(!(IntuitionBase = OpenLibrary("intuition.library", 0l)))
  103.         die(2);
  104.     if(!(s = OpenScreen(&ss)))
  105.         die(4);
  106.     ww.Screen = s;
  107.     if(!(w = OpenWindow(&ww)))
  108.         die(4);
  109.  
  110.     /* Set up a buffer for the AreaMove, AreaDraw, and AreaEnd commands */
  111.  
  112.     InitArea(&myAreaInfo, areabuffer, 100l);
  113.     w->RPort->AreaInfo = &myAreaInfo;
  114.     tmpras.RasPtr = (BYTE *) AllocRaster(640l, 400l);
  115.     tmpras.Size = (long) RASSIZE(640l, 400l);
  116.     w->RPort->TmpRas = &tmpras;
  117. }
  118.  
  119. drawstuff()
  120. {
  121.     struct RastPort *r;
  122.     register long loop;
  123.     long red, green, blue, x[10], y[10];
  124.  
  125.     r = w->RPort;
  126.     msg = GetMsg(w->UserPort);
  127.     while(msg->Class != CLOSEWINDOW)
  128.     {
  129.         msg = GetMsg(w->UserPort);
  130.  
  131.         /* draw with 2 colors this time, and just one the next to */
  132.         /* get all 3 colors modified. */
  133.  
  134.         SetDrMd(r, JAM2);
  135.         SetAPen(r, (long)(random(16) + 32));
  136.         SetBPen(r, (long)(random(16) + 48));
  137.  
  138.         /* use 1L as the last parameter if you want to use a 2 word */
  139.         /* pattern, which will give sevier jaggies. */
  140.  
  141.         SetAfPt(r, &a, 0L);
  142.  
  143.         /* Do a 5 point polygon */
  144.  
  145.         for(loop = 0l; loop < 5l; loop++)
  146.         {
  147.             x[loop] = (long)random(w->Width - w->BorderLeft
  148.                 - w->BorderRight) + w->BorderLeft;
  149.             y[loop] = (long)random(w->Height - w->BorderTop
  150.                 - w->BorderBottom) + w->BorderTop;
  151.             if(loop == 0l)
  152.                 AreaMove(r, x[loop], y[loop]);
  153.             else
  154.                 AreaDraw(r, x[loop], y[loop]);
  155.         }
  156.         AreaEnd(r);
  157.  
  158.         SetDrMd(r, JAM1);
  159.         SetAPen(r, (long)(random(16) + 16));
  160.         SetAfPt(r, &b, 0L);
  161.         for(loop = 0l; loop < 5l; loop++)
  162.         {
  163.             if(loop == 0l)
  164.                 AreaMove(r, x[loop], y[loop]);
  165.             else
  166.                 AreaDraw(r, x[loop], y[loop]);
  167.         }
  168.         AreaEnd(r);
  169.     }
  170. }
  171.  
  172. /****************************************************************
  173.  * I don't think this random number will set any speed records, but
  174.  * it is guaranteed to be as random as you can get.
  175.  ****************************************************************/
  176. random(max)
  177. int max;
  178. {
  179.     static ULONG num;
  180.     ULONG sec, mic;
  181.  
  182.     CurrentTime(&sec, &mic);
  183.     num *= sec;
  184.     num += mic;
  185.     while(num > 32000)
  186.         num = num >> 1;
  187.     return((int)(num % (ULONG)max));
  188. }
  189.  
  190. die(kind)
  191. int kind;
  192. {
  193.     static char *msgs[] = {    "",
  194.         /* err 1 */    "Unable to open graphics library.\n",
  195.         /* err 2 */    "Unable to open intuition library.\n",
  196.         /* err 4 */    "Unable to open a window.\n",
  197.                 "\n"
  198.                 };
  199.  
  200.     if(kind)        puts(msgs[kind]);
  201.     if(tmpras.RasPtr)    FreeRaster(tmpras.RasPtr,640l,400l);
  202.     if(w)            CloseWindow(w);
  203.     if(s)            CloseScreen(s);
  204.     if(GfxBase)        CloseLibrary(GfxBase);
  205.     if(IntuitionBase)    CloseLibrary(IntuitionBase);
  206.     exit(kind);
  207. }
  208.  
  209.